const _ = require('lodash');
const moment = require("moment");

/**
 * 查询对象
 */
exports.pagination = function ({params, filters, likes, order, attributes}) {
    let where = {};
    //过滤属性
    if (filters) {
        for (let i in filters) {
            let filter = filters[i];
            let attr_value = params[filter];
            if ((attr_value || attr_value === 0) && attr_value != 'all') {
                where[filter] = attr_value;
            }
        }
    }
    //搜索条件
    if (params.keyword && likes) {
        let likes_where = {};
        for (let i in likes) {
            let like = likes[i];
            likes_where[like] = {
                $like: '%' + params.keyword + '%'
            };
        }
        where['$or'] = likes_where;
    }
    //创建时间过虑
    let start = params.start_created, end = params.end_created;
    if (start && end) {
        where.created_at = {$gte: start, $lte: end};
    }
    //查询对象
    let query = {
        raw: true,
        where: where,
        offset: params.offset ? parseInt(params.offset) : 0,
        limit: params.limit ? parseInt(params.limit) : 20,
        order: order || [[params.order_key || 'id', params.order_value || 'asc']]
    };
    if (attributes) {
        query.attributes = attributes;
    }
    return query;
};

/**
 * 属性权限
 * @param body 需要处理的 json 对象
 * @param model 包含权限信息 permissionMap
 * @param permission 权限: 如 "user" 或 "admin"
 * @param include 扩展属性,如 "密码"
 * @param exclude 删除属性,如 "id"
 */
exports.permissions = function ({body, model, permission, include, exclude}) {
    let permissions = model.permissionMap[permission];
    let newBody = {};
    for (let i in permissions) {
        let key = permissions[i];
        if (body[key] !== undefined) {
            newBody[key] = body[key];
        }
    }
    if (include) {
        for (let key in include) {
            if (include[key] !== undefined) {
                newBody[key] = include[key];
            }
        }
    }
    if (exclude) {
        for (let key in exclude) {
            delete newBody[exclude[key]];
        }
    }
    return newBody;
};

/**
 * 处理post提交参数
 */
exports.handleBody = function ({req, model}) {
    let body = req.body;
    if (req.method === 'POST') {
        if (body.id) {
            body.id = parseInt(body.id);
        }
        for (let j in body) {
            if (_.isString(body[j])) {
                body[j] = _.trim(body[j]);
            }
        }
        if (model) {
            let attributes = model.attributes;
            for (let i in body) {
                let value = body[i], attribute = attributes[i];
                if (value === '' && attribute) {
                    if (attribute.allowNull) {
                        body[i] = null;
                    } else if (attribute.defaultValue !== undefined) {
                        delete body[i];
                    }
                } else if (value && attribute) {
                    let type = attribute.type.key;
                    if (type === 'DATE') {
                        try {
                            body[i] = moment(body[i]).toDate();
                        } catch (e) {
                            console.error('无效日期格式', body[i]);
                        }
                        if (body[i] == 'Invalid Date') {
                            delete body[i];
                        }
                    } else if (type === 'INTEGER') {
                        body[i] = parseInt(body[i]);
                    } else if (type === 'DECIMAL') {
                        body[i] = parseFloat(body[i]);
                    }
                }
            }
        }
    }
};